package com.emily.test.interceptor;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import org.slf4j.Logger;

import java.util.List;

/**
 * @author ：zc
 * @date ：Created in 2019/3/16 17:36
 * @description：
 */
@Configuration
public class MyAdapter implements WebMvcConfigurer {
    private Logger logger = LoggerFactory.getLogger(MyAdapter.class);
    @Bean
    public AccessInterceptor getAccessInterceptor(){
        System.out.println("注入了AccessInterceptor");
        return new AccessInterceptor();
    }

    //注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAccessInterceptor()).addPathPatterns("/**").excludePathPatterns("/webjars/**","/swagger-resources/**","/swagger-ui.html","/tResourceController/upload",
                "/tUserController/userRegister","/TCompanyController/addTCompanyInfo","/HaihuiApi/**");
        logger.info("============添加拦截器============");

    }


    //支持跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        logger.info("============跨域============");
        registry.addMapping("/**")
                .allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token")
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowCredentials(true);

    }
//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
//        FastJsonConfig config = new FastJsonConfig();
//        config.setSerializerFeatures(SerializerFeature.WriteNullListAsEmpty,
//                SerializerFeature.WriteMapNullValue,
//                SerializerFeature.WriteNullStringAsEmpty,
//                SerializerFeature.WriteNullBooleanAsFalse,
//                SerializerFeature.PrettyFormat);
//        converters.add(fastJsonHttpMessageConverter);
//    }


}
